package com.hele.sts.pub;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;


public class StringUtil
{
  public static String changeDataToCN(String strNum)
  {
    String res = "";
    for (int i = 0; i < strNum.length(); i++)
    {
      char a = strNum.charAt(i);
      switch (a)
      {
      case '0': 
        res = res + "零";
        break;
      case '1': 
        res = res + "一";
        break;
      case '2': 
        res = res + "二";
        break;
      case '3': 
        res = res + "三";
        break;
      case '4': 
        res = res + "四";
        break;
      case '5': 
        res = res + "五";
        break;
      case '6': 
        res = res + "六";
        break;
      case '7': 
        res = res + "七";
        break;
      case '8': 
        res = res + "八";
        break;
      case '9': 
        res = res + "九";
      }
    }
    return res;
  }
  
  public static String changeIntToCN(String strNum)
  {
    if (strNum.equals("0")) {
      return "零";
    }
    String res = "";
    int i = strNum.length() - 1;
    for (int j = 0; i >= 0; j++)
    {
      char a = strNum.charAt(i);
      String end = "";
      switch (j)
      {
      case 1: 
        end = "十";
        break;
      case 2: 
        end = "百";
        break;
      case 3: 
        end = "千";
        break;
      case 4: 
        end = "万";
        break;
      case 5: 
        end = "十";
        break;
      case 6: 
        end = "百";
        break;
      case 7: 
        end = "千";
        break;
      case 8: 
        end = "亿";
      }
      if (j == 1) {
        switch (a)
        {
        case '0': 
          if ((res.length() <= 1) || (res.substring(0, 1).equals("零")) || (res.substring(0, 1).equals("万"))) {
            break;
          }
          res = "零" + res;
          break;
        case '1': 
          res = end + res;
          break;
        case '2': 
          res = "二" + end + res;
          break;
        case '3': 
          res = "三" + end + res;
          break;
        case '4': 
          res = "四" + end + res;
          break;
        case '5': 
          res = "五" + end + res;
          break;
        case '6': 
          res = "六" + end + res;
          break;
        case '7': 
          res = "七" + end + res;
          break;
        case '8': 
          res = "八" + end + res;
          break;
        case '9': 
          res = "九" + end + res;
        }
      } else {
        switch (a)
        {
        default: 
          break;
        case '0': 
          if ((res.length() > 1) && (!res.substring(0, 1).equals("零")) && (!res.substring(0, 1).equals("万"))) {
            res = "零" + res;
          } else if (j == 4) {
            res = end + res;
          }
          break;
        case '1': 
          res = "一" + end + res;
          break;
        case '2': 
          res = "二" + end + res;
          break;
        case '3': 
          res = "三" + end + res;
          break;
        case '4': 
          res = "四" + end + res;
          break;
        case '5': 
          res = "五" + end + res;
          break;
        case '6': 
          res = "六" + end + res;
          break;
        case '7': 
          res = "七" + end + res;
          break;
        case '8': 
          res = "八" + end + res;
          break;
        case '9': 
          res = "九" + end + res;
        }
      }
      i--;
    }
    return res;
  }
  
  public static String strInsertE(String sSourceString, int interval, String sAddString)
  {
    if ((sSourceString != null) && (sAddString != null))
    {
      int i = 0;
      String res = "";
      String sF = "";
      if ((sSourceString.length() > 0) && (sSourceString.substring(0, 1).equals("-")))
      {
        sF = "-";
        sSourceString = sSourceString.substring(1);
      }
      if (sSourceString.lastIndexOf(".") > 0)
      {
        int dot = sSourceString.lastIndexOf(".");
        res = sSourceString.substring(dot);
        sSourceString = sSourceString.substring(0, dot);
      }
      for (int len = sSourceString.length(); len > interval; len = sSourceString.length())
      {
        res = sAddString + sSourceString.substring(len - interval) + res;
        sSourceString = sSourceString.substring(0, len - interval);
      }
      return sF + sSourceString + res;
    }
    return sSourceString;
  }
  
  public static String stringReplace(String sSourceString, String sOldString, String sNewString, boolean bReplaceFirstInstanceOnly)
  {
    if ((sOldString != sNewString) && (sSourceString != null) && (sOldString != null))
    {
      if (sNewString == null) {
        sNewString = "";
      }
      StringBuffer oResult = new StringBuffer();
      int len = sSourceString.length();
      int i = 0;
      for (int iPos = sSourceString.indexOf(sOldString, i); iPos != -1; iPos = sSourceString.indexOf(sOldString, i))
      {
        oResult.append(sSourceString.substring(i, iPos)).append(sNewString);
        i = iPos + sOldString.length();
        if (bReplaceFirstInstanceOnly) {
          break;
        }
      }
      oResult.append(sSourceString.substring(i));
      return oResult.toString();
    }
    return sSourceString;
  }
  
  public static String myUtf(String s)
    throws Exception
  {
    byte[] bts = s.getBytes("UTF-8");
    int len = bts.length;
    byte[] ret1 = new byte[len];
    int j = 0;
    for (int i = 0; i < bts.length;)
    {
      if (i + 2 < bts.length)
      {
        if (bts[(i + 1)] == -65)
        {
          ret1[j] = ((byte)(bts[(i + 2)] + 64));
          i += 3;
        }
        else if (bts[(i + 1)] == -66)
        {
          ret1[j] = bts[(i + 2)];
          i += 3;
        }
        else
        {
          ret1[j] = bts[i];
          i++;
        }
      }
      else
      {
        ret1[j] = bts[i];
        i++;
      }
      j++;
    }
    return new String(ret1, 0, j);
  }
  
  public static String createLogId()
  {
//    String log = String.valueOf(System.currentTimeMillis());
//    int len = log.length();
//    if (len > 12) {
//      return log.substring(0,len - 12);
//    }
    return RandomStringUtils.randomNumeric(12);
    //return log;
  }
  
  public static String createUserId()
  {
    String userId = String.valueOf(System.currentTimeMillis());
    return "999999" + userId.substring(userId.length() - 10, userId.length());
  }
  
  public static String codeNumStr(String in, String mdl)
  {
    String rtn = "-1";
    if ((mdl == null) || (in == null)) {
      return rtn;
    }
    int inLen = in.length();
    int maxLen = mdl.length();
    if (inLen <= maxLen)
    {
      int x = maxLen - inLen;
      rtn = mdl.substring(0, x) + in;
    }
    return rtn;
  }
  
  public static String randomStrWithDatetime(){
	  return String.valueOf(System.currentTimeMillis());
  }
  
  
  public static void main(String[] s)
  {
    System.out.println(strInsertE("-123456.abc", 3, ","));
    System.out.println(changeIntToCN("0"));
    System.out.println(codeNumStr("23", "0"));
    System.out.println(randomStrWithDatetime());
    System.out.println(createLogId());
  }
}
